import django.http
from django.shortcuts import render
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet
from .models import *
from .serializer import QTypeModelSerializer,QuestionModelSerializer,UsersModelSerializer,GroupModelSerializer,MenuModelSerializer
from django.contrib.auth.models import Group
from django.http import JsonResponse
from job.MyFilter import AdminUserFilters,RoleFilter

# Create your views here.


# 用户视图集
class UsersModelViewSet(ModelViewSet):
    queryset = Users.objects.all()
    serializer_class = UsersModelSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ('username',)
    # filterset_class = AdminUserFilters


# 题型视图集
class QTypeModelViewSet(ModelViewSet):
    queryset = QType.objects.all()
    serializer_class = QTypeModelSerializer
    # pagination_class = None
    @action(methods=['get'],detail=False)
    def all(self,request):
        return JsonResponse({'data':list(QType.objects.all().values())})


# 题目视图集
class QuestionModelViewSet(ModelViewSet):
    queryset = Question.objects.all()
    serializer_class = QuestionModelSerializer
    filter_backends = [DjangoFilterBackend]
    # filterset_fields = ('type',)



# 角色视图集
class RoleModelViewSet(ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupModelSerializer
    filter_backends = [DjangoFilterBackend]
    # filterset_fields = ('name',)
    filterset_class = RoleFilter
    pagination_class = None

# 菜单视图集

class MenuModelViewSet(ModelViewSet):
    queryset = Menu.objects.all()
    serializer_class = MenuModelSerializer
    pagination_class = None